const puppeteer = require('puppeteer');
const chalk = require('chalk');
const mongoose = require('mongoose');

const log = console.log;


(async () => {
  mongoose.connect('mongodb://localhost/movie');
  var schema = mongoose.Schema
  var userSchema = new schema({
    name:{
      type:String,
      required:true
    },
    type:{
      type:String
    },
    img:{
      type:String
    },
    urlId:{
      type:Number,
      required:true
    },
    area:{
      type:String
    },
    year:{
      type:Number
    },
    detail:{
      type:String
    },
    actor:{
      type:Array
    },
    director:{
      type:Array
    },
    cols:{
      type:Array
    }
  })
  var Movie = mongoose.model('movies',userSchema)

  let movies = []
  let num = 1;
  let pages = 123;
  const baseUrl = 'http://www.vip1280.net/'
  const movieUrl = 'http://www.vip1280.net/frim/index4-'
  // const backUrl = '&searchtype=5&tid=1&area=%E7%BE%8E%E5%9B%BD'
  // http://www.vip1280.net/search.php?page=2&searchtype=5&tid=2&area=%E5%A4%A7%E9%99%86
  const backUrl = '.html'
  const browser = await puppeteer.launch( {headless: false });
  var Page = await browser.newPage();
  // 进入页面
 
  
  for (let page = 1; page < pages; page++) {
    var urlCol= movieUrl + page + backUrl
    log(chalk.green(urlCol))
    await Page.waitFor(1000)
    try {
      await Page.goto(urlCol,{timeout:0});
    } catch (error) {
      await Page.waitFor(60000)
      await Page.goto(urlCol,{timeout:0});
    }
    const lis = await Page.$$eval('div.stui-pannel_bd > ul > li > .stui-vodlist__box>a',function(e) {
        let arr = []   
        e.forEach(element => {
          let obj = {}
          obj.urlId = element.getAttribute('href').match(/\d+/g)[0]
          obj.img = element.getAttribute('data-original')
          obj.name = element.getAttribute('title')
          arr.push(obj)
         });
    
        return arr
    });
   
    for (let i = 0; i < lis.length; i++) {
      let element = lis[i];
      var url = baseUrl + 'play/' + element.urlId + '-0-0.html'
      await Page.waitFor(1000)
      try {
        await Page.goto(url,{timeout:0});
      } catch (error) {
        await Page.waitFor(60000)
        await Page.goto(url,{timeout:0});
      }
      // const roots = await Page.$$eval('ul.stui-content__playlist',function(e) {return e});
      // let Num = roots.length;
      element.type = await Page.$eval('div.stui-player__detail.detail > p > a:nth-child(6)',e=>e.innerHTML)
      element.area = await Page.$eval('div.stui-player__detail.detail > p > a:nth-child(9)',e=>e.innerHTML)
      element.year = await Page.$eval('div.stui-player__detail.detail > p > a:nth-child(12)',e=>e.innerHTML)
      element.detail = await Page.$eval('div.stui-player__detail.detail > div.detail-content > p.desc.margin-0',e=>e.innerText)
      element.actor = await Page.$$eval('.detail-content > p:nth-child(1)>a',function (e) {
        let arr = []
        e.forEach(ele => {
          arr.push(ele.innerText)
        });
        return arr
      })
      element.director = await Page.$$eval('div.stui-player__detail.detail > div.detail-content > p:nth-child(2)>a',function (e) {
        let arr = []
        e.forEach(ele => {
          arr.push(ele.innerText)
        });
        return arr
      })

      let chese = '.stui-content__playlist:nth-child(1)>li> a'
      element.cols  = await Page.$$eval(chese,function(e) {
        let arrAll = [] 
        e.forEach(ele => {
          let arr = []
          let re1 = new RegExp('-1-');
          let re0 = new RegExp('-0-');
          let href = ele.getAttribute('href')
          if(re0.test(href)){
            arr.push(ele.getAttribute('title'))
            arr.push('http://www.vip1280.net' + ele.getAttribute('href'))
            arr.push('col0')
            arrAll.push(arr)
          }else if(re1.test(href)){
            arr.push(ele.getAttribute('title'))
            arr.push('http://www.vip1280.net' + ele.getAttribute('href'))
            arr.push('col1')
            arrAll.push(arr)
          }else{
            
          }
          
         
        });
        return arrAll
      }); 
      console.log(chalk.yellow(element.cols),888);
      await rangeCols(element,page)
      
    }

  }
  


  async function rangeCols(element,page) {
    let colArr = element.cols;
    // log(colArr)
    for (let index = 0; index < colArr.length; index++) {
      const col = colArr[index];
      log(col[1])
      await Page.waitFor(1000)
      try {
        await Page.goto(col[1],{timeout:0})
      } catch (error) {
        await Page.waitFor(60000)
        await Page.goto(col[1],{timeout:0})
      }
      let playUrl = await Page.$eval('.stui-player__video',function(e) {
        return e.innerHTML
      })
      playUrl = playUrl.match(/<script>var now="(.*?)"/)[1]
      log(chalk.green(playUrl))
      await element.cols[index].push(playUrl)
    }
    for (const key in element) {
      log(element[key],key)
      
    }

    let addOne = new Movie(element)
    try {
        addOne.save().then((ret)=>{
          if(ret){
            log(chalk.red('成功一条！当前进度'+ page + '页,第'+num+'条'))
            num++
          }
        })
    } catch (error) {
        log('保存失败')
        return
       
        
    }
     
  }










 

  await browser.close()

  
})()